package cn.com.smartdevices.bracelet.chart;

import android.annotation.SuppressLint;
import android.content.Context;
import android.graphics.RectF;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.animation.AccelerateDecelerateInterpolator;

import com.goodving.guli.bt.profile.scale.GuliLog;
import com.goodving.guli.view.C3657a;
import com.goodving.guli.view.C4220a;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
import java.util.List;

import cn.com.smartdevices.bracelet.chart.base.BarChart;
import cn.com.smartdevices.bracelet.chart.base.BaseChartView;
import cn.com.smartdevices.bracelet.chart.base.C0759p;
import cn.com.smartdevices.bracelet.chart.base.ChartScroller;
import cn.com.smartdevices.bracelet.chart.base.Renderer;
import cn.com.smartdevices.bracelet.chart.data.ChartDataLoader;
import cn.com.smartdevices.bracelet.chart.data.ItemData;

public class DynamicDetailChartView extends BaseChartView<DynamicDetailChart> {
    private static final String f3859a = "Chart.DynamicDetailChartView";
    private int f3860A;
    private DynamicDetailChartLoadCallback f3861B;
    private ChartScroller.ScrollingListener f3862C;
    private Renderer f3863b;
    private Renderer f3864c;
    private Renderer f3865d;
    private Renderer f3866q;
    private Renderer f3867r;
    private Renderer f3868s;
    private Renderer f3869t;
    private Renderer f3870u;
    private Renderer f3871v;
    private Renderer f3872w;
    private ChartScroller f3873x;
    private int f3874y;
    private ArrayList<Integer> f3875z;



    public DynamicDetailChartView(Context context) {
        this(context, null);

    }

    public DynamicDetailChartView(Context context, AttributeSet attributeSet) {
        this(context, attributeSet, 0);
    }

    public DynamicDetailChartView(Context context, AttributeSet attributeSet, int i) {
        super(context, attributeSet, i);
        this.f3862C = new C0767i(this);
        this.f3863b = new C0759p(1308622847);
        this.f3864c = new C0759p(-2130706433);
        this.f3865d = new C0759p(-1711303168);
        this.f3866q = new C0759p(452984831);
        this.f3867r = new C0759p(-855638017);
        this.f3868s = new C0759p(-855638017);
        this.f3869t = new C0759p(-27136);
        this.f3870u = new C0759p(872415231);
        this.f3871v = new C0759p(1308622847);
        this.f3872w = new C0759p(-855638017);
        this.f3847e = new DynamicDetailChart(context);
        ((DynamicDetailChart) this.f3847e).setParentView((BaseChartView) this);
        this.f3855m = new AccelerateDecelerateInterpolator();
        this.f3873x = new ChartScroller(getContext(), this.f3862C);
        this.f3875z = new ArrayList();
    }

    class C0765g implements ChartDataLoader.LoadCallback {
        final /* synthetic */ DynamicDetailChartView f3992a;

        C0765g(DynamicDetailChartView dynamicDetailChartView) {
            this.f3992a = dynamicDetailChartView;
        }

        public void onDataLoaded(ItemData c0737e) {
            if (c0737e != null) {
                DynamicDetailChartData c0768j = (DynamicDetailChartData) c0737e;
                this.f3992a.f3875z.remove(Integer.valueOf(c0737e.f3897g));
                if (this.f3992a.f3875z.isEmpty()) {
                    onToItem(this.f3992a.f3874y);
                    this.f3992a.m8409a(c0768j);
                    this.f3992a.b_();
                    this.f3992a.f3861B.onDataLoaded(c0737e);
                }
            }
        }

        public boolean hasData(int i) {
            return this.f3992a.f3861B.hasData(i);
        }

        public ItemData loadData(int i) {
            return this.f3992a.f3861B.loadData(i);
        }

        public void onToItem(int i) {
            this.f3992a.f3861B.onToItem(i);
        }
    }

    class C0766h implements ChartDataLoader.LoadCallback {
        final /* synthetic */ DynamicDetailChartView f3993a;

        C0766h(DynamicDetailChartView dynamicDetailChartView) {
            this.f3993a = dynamicDetailChartView;
        }

        public void onDataLoaded(ItemData c0737e) {
        }

        public boolean hasData(int i) {
            return this.f3993a.f3861B.hasData(i);
        }

        public ItemData loadData(int i) {
            return this.f3993a.m8422b(i);
        }

        public void onToItem(int i) {
            this.f3993a.f3861B.onToItem(i);
        }
    }

    class C0767i implements ChartScroller.ScrollingListener {
        final /* synthetic */ DynamicDetailChartView f3994a;

        C0767i(DynamicDetailChartView dynamicDetailChartView) {
            this.f3994a = dynamicDetailChartView;
        }

        public void onStart () { // m8487a
            GuliLog.tracePrintInfo("Chart.DynamicDetailChartView", "Scroll OnStart!!");
        }

        public boolean onScrollX (int i) { // m8488a
            return false;
        }

        public void onJustify () { // m8489b
            GuliLog.tracePrintInfo("Chart.DynamicDetailChartView", "Scroll OnJustify!!");
        }

        @SuppressLint({"NewApi"})
        public boolean onScrollY (int i) { // m8490b
            int s = (int) ((DynamicDetailChart) this.f3994a.f3847e).getScroll();
            int h = (int) ((DynamicDetailChart) this.f3994a.f3847e).getEdgeLength();
            GuliLog.tracePrintInfo("Chart.DynamicDetailChartView", "Scrolled : " + s + " Edge : " + h + " Distance : " + i);
            if ((s == 0 && i > 0) || (s == h && i < 0)) {
                return false;
            }
            ((DynamicDetailChart) this.f3994a.f3847e).scroll((float) i);
            this.f3994a.postInvalidateOnAnimation();
            return true;
        }

        public void onFinish () { // m8491c
            GuliLog.tracePrintInfo("Chart.DynamicDetailChartView", "Scroll OnFinish!!");
        }
    }


    public class DynamicDetailChartData  extends ItemData { // C0768j
        public String f3995a;
        public String f3996b;
        public List<DynamicDetailChartSleepData> f3997c;
        public List<DynamicDetailChartStepData> f3998d;

        public DynamicDetailChartData() {
            this.f3995a = C3657a.f18328f;
            this.f3996b = C3657a.f18328f;
            this.f3997c = new ArrayList();
            this.f3998d = new ArrayList();
        }
    }

    public interface DynamicDetailChartLoadCallback  extends ChartDataLoader.LoadCallback { // C0769k
        void onTouchNothing (float f, float f2); // m8698a

        void onTouchItem (int i, BarChart.BarItem c0726c, int i2, float f, float f2); // m8699a
    }

    public class DynamicDetailChartSleepData  { // C0770l
        public static final int MODE_SLEEP_DEEP  = 3; //f3999a
        public static final int MODE_SLEEP_SHALLOW  = 2;//f4000b
        public static final int MODE_SLEEP_ACTIVE = 1; // f4001c
        public static final int MODE_SLEEP_USER  = 4; //f4002d
        public int f4003e;
        public int f4004f;
        public int f4005g;
    }

    public class DynamicDetailChartStepData  {// C0772n
        public int f4006a;
        public int f4007b;
    }

    private void m8409a(DynamicDetailChartData c0768j) {
        if (this.f3860A == 16) {
            m8419a(c0768j.f3997c);
        }
        if (this.f3860A == 1) {
            m8424b(c0768j.f3998d);
        }
    }

    public ItemData m8415a(int i, int... iArr) {
        int intValue;
        this.f3874y = i;
        ItemData b = this.f3848f.getItemData(i);
        if (b == null && this.f3848f.hasItemData(i)) {
            this.f3875z.add(Integer.valueOf(i));
        }
        if (iArr != null) {
            for (int i2 : iArr) {
                if (this.f3848f.getItemData(i2) == null && this.f3848f.hasItemData(i2)) {
                    this.f3875z.add(Integer.valueOf(i2));
                }
            }
        }
        Iterator it = this.f3875z.iterator();
        while (it.hasNext()) {
            intValue = ((Integer) it.next()).intValue();
            this.f3848f.putItemData(intValue, new DynamicDetailChartData());
            this.f3848f.loadItemData(intValue);
        }
        return b;
    }

    public void m8416a(int i) {
        this.f3860A = i;
        ((DynamicDetailChart) this.f3847e).setMode(this.f3860A);
    }

    public void m8417a(int i, int i2) {
        ((DynamicDetailChart) this.f3847e).setStartEndTimeIndex(i, i2);
    }

    public void m8418a(ChartDataLoader c0746d) {
        this.f3848f = c0746d;
        this.f3861B = (DynamicDetailChartLoadCallback) c0746d.getLoadCallback();
        this.f3848f.setLoadCallback(new C0765g(this));
        ((DynamicDetailChart) this.f3847e).setLoadCallback(new C0766h(this));
    }

    public void m8419a(List<DynamicDetailChartSleepData> list) {
        Collection arrayList = new ArrayList();
        for (DynamicDetailChartSleepData c0770l : list) {
            C0762d c0762d = new C0762d(getContext());
            c0762d.f3884c = c0770l.f4004f;
            c0762d.f3885d = c0770l.f4005g;
            c0762d.f3883b = c0770l.f4003e;
            switch (c0770l.f4003e) {
                case 1 /*1*/:
                    c0762d.setRenderer(this.f3865d);
                    break;
                case C4220a.f20888k /*2*/:
                    c0762d.setRenderer(this.f3864c);
                    break;
                case C4220a.f20889l /*3*/:
                    c0762d.setRenderer(this.f3863b);
                    break;
                case C4220a.aQ /*4*/:
                    c0762d.setRenderer(this.f3866q);
                    break;
                default:
                    break;
            }
            arrayList.add(c0762d);
        }
        List arrayList2 = new ArrayList();
        arrayList2.addAll(((DynamicDetailChart) this.f3847e).getSleepChart().getItems());
        arrayList2.addAll(arrayList);
        ((DynamicDetailChart) this.f3847e).fillSleepData(arrayList2);
    }

    public void m8420a(boolean z) {
        super.refresh(z);
    }

    public boolean m8421a(float f, float f2) {
        boolean z = false;
        if (this.f3860A == 16) {
            boolean z2 = false;
            for (BarChart.BarItem c0726c : ((DynamicDetailChart) this.f3847e).getSleepChart().getItems()) {
                switch (c0726c.f3883b) {
                    case 1 /*1*/:
                        c0726c.setRenderer(this.f3865d);
                        break;
                    case C4220a.f20888k /*2*/:
                        c0726c.setRenderer(this.f3864c);
                        break;
                    case C4220a.f20889l /*3*/:
                        c0726c.setRenderer(this.f3863b);
                        break;
                    case C4220a.aQ /*4*/:
                        c0726c.setRenderer(this.f3866q);
                        break;
                }
                RectF m = c0726c.getRect();
                if (f >= m.left && f <= m.right) {
                    switch (c0726c.f3883b) {
                        case 1 /*1*/:
                            c0726c.setRenderer(this.f3869t);
                            break;
                        case C4220a.f20888k /*2*/:
                            c0726c.setRenderer(this.f3868s);
                            break;
                        case C4220a.f20889l /*3*/:
                            c0726c.setRenderer(this.f3867r);
                            break;
                        case C4220a.aQ /*4*/:
                            c0726c.setRenderer(this.f3870u);
                            break;
                    }
                    this.f3861B.onTouchItem(c0726c.f3884c, c0726c, c0726c.f3884c + ((int) (((f - m.left) / m.width()) * ((float) c0726c.f3885d))), f, f2);
                    z2 = true;
                }
            }
            m8425c();
            return z2;
        } else if (this.f3860A != 1) {
            return false;
        } else {
            for (BarChart.BarItem c0726c2 : ((DynamicDetailChart) this.f3847e).getStepChart().getItems()) {
                c0726c2.setRenderer(this.f3871v);
                RectF m2 = c0726c2.getRect();
                if (f >= m2.left && f <= m2.right) {
                    c0726c2.setRenderer(this.f3872w);
                    this.f3861B.onTouchItem(c0726c2.f3884c, c0726c2, c0726c2.f3884c * 60, f, f2);
                    z = true;
                }
            }
            m8425c();
            return z;
        }
    }

    public ItemData m8422b(int i) {
        return m8415a(i, null);
    }

    public void m8423b() {
        this.f3875z.clear();
        this.f3848f.clearData();
        ((DynamicDetailChart) this.f3847e).clearStepData();
        ((DynamicDetailChart) this.f3847e).clearSleepData();
        ((DynamicDetailChart) this.f3847e).notifyChanged();
    }

    public void m8424b(List<DynamicDetailChartStepData> list) {
        List arrayList = new ArrayList();
        for (DynamicDetailChartStepData c0772n : list) {
            C0762d c0762d = new C0762d(getContext());
            c0762d.f3883b = c0772n.f4007b;
            c0762d.f3884c = c0772n.f4006a;
            c0762d.f3885d = 1;
            c0762d.setRenderer(this.f3871v);
            arrayList.add(c0762d);
        }
        ((DynamicDetailChart) this.f3847e).addStepData(arrayList);
    }

    public void b_() {
        ((DynamicDetailChart) this.f3847e).notifyChanged();
    }

    public void m8425c() {
        if (!animRefresh().isStarted()) {
            super.refresh();
        }
    }

    public void m8426c(int i) {
        ((DynamicDetailChart) this.f3847e).setOffset(i);
    }

    public void m8427d(int i) {
        for (BarChart.BarItem c0726c : ((DynamicDetailChart) this.f3847e).getStepChart().getItems()) {
            if (c0726c.f3884c == i) {
                c0726c.setRenderer(this.f3872w);
                return;
            }
        }
    }

    public boolean onTouchEvent(MotionEvent motionEvent) {
        if (!this.f3849g) {
            return false;
        }
        if (getParent() != null) {
            getParent().requestDisallowInterceptTouchEvent(true);
        }
        float x = motionEvent.getX();
        float y = motionEvent.getY();
        switch (motionEvent.getAction()) {
            case C4220a.f20886i /*0*/:
            case C4220a.f20888k /*2*/:
                if (m8421a(x, y)) {
                    return true;
                }
                this.f3861B.onTouchNothing(x, y);
                return true;
            case 1 /*1*/:
            case C4220a.f20889l /*3*/:
                m8421a((float) -1, (float) -1);
                this.f3861B.onTouchNothing(x, y);
                return true;
            default:
                return true;
        }
    }
}
